{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "def getFileName(filepath):\n",
    "    file_list = []\n",
    "    for root,dirs,files in os.walk(filepath):\n",
    "        for filespath in files:\n",
    "            if 'pdf' in filespath.split('.')[1]:\n",
    "                file_list.append(os.path.join(root,filespath))\n",
    "    return file_list\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import sys\n",
    "import io\n",
    "from pdfminer.pdfpage import PDFPage\n",
    "# from pdfminer.converter import TextConverter\n",
    "from pdfminer.pdfparser import PDFParser\n",
    "from pdfminer.pdfdocument import PDFDocument\n",
    "from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter\n",
    "from pdfminer.layout import LAParams\n",
    "from pdfminer.converter import PDFPageAggregator\n",
    "# from pdfminer.pdfinterp import PDFTextExtractionNotAllowed\n",
    "\n",
    "# 解析文本用到的类：\n",
    "# PDFParser（文档分析器）：从文件中获取数据\n",
    "# PDFDocument（文档对象）：保存文件数据\n",
    "# PDFPageInterpreter（解释器）：处理页面内容\n",
    "# PDFResourceManager（资源管理器）：存储共享资源\n",
    "# PDFDevice:将解释器处理好的内容转换为我们所需要的\n",
    "# PDFPageAggregator（聚合器）:读取文档对象\n",
    "# LAParams（参数分析器）\n",
    "\n",
    "# convert one PDF file to TXT file\n",
    "def onePdfToTxt(filepath, outpath):\n",
    "    try:\n",
    "        #rb以二进制读模式打开本地pdf文件\n",
    "        fp = open(filepath, 'rb')\n",
    "        outfp = open(outpath, 'w', encoding='utf-8')\n",
    "        #创建一个pdf文档分析器\n",
    "        parser = PDFParser(fp)\n",
    "        #创建一个PDF文档\n",
    "        doc= PDFDocument(parser)\n",
    "        # 连接分析器 与文档对象\n",
    "        # parser.set_document(doc)\n",
    "        # doc.set_parser(parser)\n",
    "        # 提供初始化密码doc.initialize(\"lianxipython\")\n",
    "        # 如果没有密码 就创建一个空的字符串\n",
    "        # doc.initialize(\"\")\n",
    "        # 检测文档是否提供txt转换，不提供就忽略\n",
    "        if not doc.is_extractable:\n",
    "            #raise PDFTextExtractionNotAllowed\n",
    "            pass\n",
    "\n",
    "        else:\n",
    "            #创建PDf资源管理器\n",
    "            resource = PDFResourceManager()\n",
    "            #创建一个PDF参数分析器\n",
    "            laparams = LAParams()\n",
    "            #创建聚合器,用于读取文档的对象\n",
    "            device = PDFPageAggregator(resource,laparams=laparams)\n",
    "            #创建解释器，对文档编码，解释成Python能够识别的格式\n",
    "            interpreter = PDFPageInterpreter(resource,device)\n",
    "            # 循环遍历列表，每次处理一页的内容 doc.get_pages() 获取page列表\n",
    "            for page in enumerate(PDFPage.create_pages(doc)):\n",
    "                #利用解释器的process_page()方法解析读取单独页数\n",
    "                interpreter.process_page(page[1])\n",
    "                #使用聚合器get_result()方法获取内容\n",
    "                layout = device.get_result()\n",
    "                #这里layout是一个LTPage对象,里面存放着这个page解析出的各种对象\n",
    "                for out in layout:\n",
    "                    #判断是否含有get_text()方法，获取我们想要的文字\n",
    "                    if hasattr(out,\"get_text\"):\n",
    "                        text=out.get_text()\n",
    "                        outfp.write(text+'\\n')\n",
    "            fp.close()\n",
    "            outfp.close()\n",
    "    except Exception as e:\n",
    "         print (e)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    onePdfToTxt(\"../PDFExtractor/Essay.pdf\", \"../PDFExtractor/Result.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import sys\n",
    "import io\n",
    "from pdfminer.pdfpage import PDFPage\n",
    "from pdfminer.pdfparser import PDFParser\n",
    "from pdfminer.pdfdocument import PDFDocument\n",
    "from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter\n",
    "from pdfminer.layout import LAParams\n",
    "from pdfminer.converter import PDFPageAggregator\n",
    "# from pdfminer.pdfinterp import PDFTextExtractionNotAllowed\n",
    "\n",
    "# convert one PDF file to TXT file\n",
    "def onePdfToTxt(filepath, outpath):\n",
    "    try:\n",
    "        fp = open(filepath, 'rb')\n",
    "        outfp = open(outpath, 'w', encoding='utf-8')\n",
    "        parser = PDFParser(fp)\n",
    "        doc= PDFDocument(parser)\n",
    "        if not doc.is_extractable:\n",
    "            pass\n",
    "        else:\n",
    "            resource = PDFResourceManager()\n",
    "            laparams = LAParams()\n",
    "            device = PDFPageAggregator(resource,laparams=laparams)\n",
    "            interpreter = PDFPageInterpreter(resource,device)\n",
    "            for page in enumerate(PDFPage.create_pages(doc)):\n",
    "                interpreter.process_page(page[1])\n",
    "                layout = device.get_result()\n",
    "                for out in layout:    \n",
    "                    if hasattr(out,\"get_text\"):\n",
    "                        text=out.get_text()\n",
    "                        outfp.write(text+'\\n')\n",
    "            fp.close()\n",
    "            outfp.close()\n",
    "    except Exception as e:\n",
    "         print (e)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    onePdfToTxt(\"../PDFExtractor/Essay.pdf\",\\\n",
    "         \"../PDFExtractor/Result.txt\")\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('py38')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "da3096a4b2ddf6e3108983d9c4f8ed6b468b54d4f76a60924a980331cf3af984"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
